package com.adamrocker.android.sharefelica;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import com.adamrocker.android.sharefelica.util.P;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

/**
 * 他のアプリから共有するときに表示するのはShareFelicaProxy
 * @author 共有コミュニケーションチーム (adamrocker, BaoBab, tarotaro)
 *
 */
public class ShareFelicaList extends ListActivity {
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.list);
        List<HashMap<String, String>> list;
        try {
            list = loadDate();
            SimpleAdapter adapter= new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, new String[] {P.TITLE, P.URI}, new int[]{android.R.id.text1, android.R.id.text2});
            setListAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<HashMap<String, String>> loadDate() throws Exception {
        InputStream is = openFileInput(P.LOGFILE);
   		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		String line = "";
		while ((line = br.readLine()) != null) {
		    try {
		        JSONObject json = new JSONObject(line);
		        HashMap<String, String> map = new HashMap<String, String>();
		        map.put(P.TITLE, json.getString(P.TITLE));
		        map.put(P.URI, json.getString(P.URI));
		        map.put(P.ACTION, json.getString(P.ACTION));
		        list.add(map);
		    } catch (Exception e) {}
		}
		br.close();
		isr.close();
		is.close();
        return list;
    }
}